package edu.zut.cs.network.entity;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQBytesMessage;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.junit.Test;

public class QueueProducer {
	@Test
    public void testQueueProducer() throws Exception {
        //1、创建一个连接工厂对象，需要指定服务的ip及端口
		ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("yrl","yrl","tcp://192.168.57.138:61616");
		connectionFactory.setTrustAllPackages(true);
       // ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.57.138:61616");
        //2、使用工厂对象创建一个Connection对象。
        Connection connection = connectionFactory.createConnection();
        //3、开启连接，调用Connection对象的start方法。
        connection.start();
        //4、创建一个Session对象。
        //第一个参数：是否开启事务。如果true开启事务，第二个参数无意义。一般不开启事务false。
        //第二个参数：应答模式。自动应答或者手动应答。一般自动应答、、Session.AUTO_ACKNOWLEDGE。
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        //Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        //5、使用Session对象创建一个Destination对象。两种形式queue、topic，现在应该使用queue
        Queue queue = session.createQueue("sqlqueue2");
        //6、使用Session对象创建一个Producer对象。
        MessageProducer producer = session.createProducer(queue);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);  

        //7、创建一个Message对象，可以使用TextMessage。
//        ActiveMQTextMessage textMessage=(ActiveMQTextMessage) session.createTextMessage("yrl");
//        producer.send(textMessage);
        Book book=new Book("wangtiebiao","f","f",2L);
        List<Book> mm=new ArrayList<>();     
        mm.add(book);
        mm.add(book);
        
        byte[] bs=serialize(mm);
        ActiveMQBytesMessage msg = (ActiveMQBytesMessage) session.createBytesMessage();
        msg.writeBytes(bs);
        //8、发送消息
        producer.send(msg);
        //session.commit();
        //9、关闭资源
        producer.close();
        session.close();
        connection.close();
    }
	public static <T> byte[] serialize(List<T> value) {
		if (value == null) {
			throw new NullPointerException("Can't serialize null");
		}
		byte[] rv=null;
		ByteArrayOutputStream bos = null;
		ObjectOutputStream os = null;
		try {
			bos = new ByteArrayOutputStream();
			os = new ObjectOutputStream(bos);
			for(T obj : value){
				os.writeObject(obj);
			}
			os.writeObject(null);
			os.close();
			bos.close();
			rv = bos.toByteArray();
		} catch (IOException e) {
			throw new IllegalArgumentException("Non-serializable object", e);
		}
		return rv;
	}

}
